package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("1111");
        resp.setContentType("application/json;charset=utf8");
        BlogDao blogDao = new BlogDao();
        List<Blog> blogs = blogDao.selectAll();
//        for (Blog blog : blogs) {
//            System.out.println("第一篇博客内容：");
//            System.out.println(blog.getPostTime());
//            System.out.println(blog.getUserId());
//            System.out.println(blog.getBlogId());
//            System.out.println(blog.getContent());
//            System.out.println(blog.getTitle());
//        }
        String jsonString = objectMapper.writeValueAsString(blogs);
        resp.getWriter().write(jsonString);

    }
}
